package leetcode.programmer;

// 面试题 01.05. 一次编辑
public class OneEditAway {

    public boolean oneEditAway(String first, String second) {
        int l1 = first.length();
        int l2 = second.length();
        if (Math.abs(l1 - l2) > 1) {
            return false;
        }
        if (l2 > l1) {
            // 保证第一个比第二个长
            return oneEditAway(second, first);
        }
        for (int i = 0; i < l2; i++) {
            if (first.charAt(i) != second.charAt(i)) {
                return first.substring(i + 1).equals(
                        second.substring(l1 == l2 ? i + 1 : i));
            }
        }
        return true;
    }
}
